#include "M451Series.h"
#include "ssd1306.h"


#define PLL_CLOCK       72000000

void HalInit(){
	  SYS_UnlockReg();

/*---------------------------------------------------------------------------------------------------------*/
    /* Init System Clock                                                                                       */
    /*---------------------------------------------------------------------------------------------------------*/

    /* Enable Internal RC 22.1184MHz clock */
    CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

    /* Waiting for Internal RC clock ready */
    CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

    /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
    CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

    /* Enable external XTAL 12MHz clock */
    CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

    /* Waiting for external XTAL clock ready */
    CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

    /* Set core clock as PLL_CLOCK from PLL */
    CLK_SetCoreClock(PLL_CLOCK);

    /* Enable UART module clock */

    /* Enable I2C0 module clock */
    CLK_EnableModuleClock(I2C0_MODULE);


    /*---------------------------------------------------------------------------------------------------------*/
    /* Init I/O Multi-function                                                                                 */
    /*---------------------------------------------------------------------------------------------------------*/

		SYS->GPD_MFPL &= ~SYS_GPD_MFPL_PD4MFP_Msk;
		SYS->GPD_MFPL |= SYS_GPD_MFPL_PD4MFP_I2C0_SDA;
		
		SYS->GPD_MFPL &= ~SYS_GPD_MFPL_PD5MFP_Msk;
		SYS->GPD_MFPL |= SYS_GPD_MFPL_PD5MFP_I2C0_SCL;
		I2C_Open(I2C0,100000);
		
    printf("I2C clock %d Hz\n", I2C_GetBusClockFreq(I2C0));

    I2C_SetSlaveAddr(I2C0, 0, 0x78, 0);   /* Slave Address : 0x15 */

    SYS_LockReg();

	//GPIO_SetMode(PB,BIT14,GPIO_PMD_INPUT);
	//GPIO_SetMode(PB,BIT9,GPIO_PMD_OUTPUT);

  //I2C_EnableInt(I2C0);
  //NVIC_EnableIRQ(I2C0_IRQn);
}

typedef void (*I2C_FUNC)(uint32_t u32Status);
static I2C_FUNC s_I2CHandlerFn = NULL;
volatile int muxI2C = 0;
uint8_t g_au8TxData[3];
uint8_t g_u8DeviceAddr = 0x50;
uint8_t g_u8DataLen;
volatile uint8_t g_u8EndFlag = 0;

/*---------------------------------------------------------------------------------------------------------*/
/*  I2C Tx Callback Function                                                                               */
/*---------------------------------------------------------------------------------------------------------*/
void I2C_MasterTx(uint32_t u32Status)
{
    if(u32Status == 0x08)                       /* START has been transmitted */
    {
        I2C_SET_DATA(I2C0, g_u8DeviceAddr << 1);    /* Write SLA+W to Register I2CDAT */
        I2C_SET_CONTROL_REG(I2C0, I2C_CTL_SI);
    }
    else if(u32Status == 0x18)                  /* SLA+W has been transmitted and ACK has been received */
    {
        I2C_SET_DATA(I2C0, g_au8TxData[g_u8DataLen++]);
        I2C_SET_CONTROL_REG(I2C0, I2C_CTL_SI);
    }
    else if(u32Status == 0x20)                  /* SLA+W has been transmitted and NACK has been received */
    {
        I2C_STOP(I2C0);
        I2C_START(I2C0);
    }
    else if(u32Status == 0x28)                  /* DATA has been transmitted and ACK has been received */
    {
        if(g_u8DataLen != 3)
        {
            I2C_SET_DATA(I2C0, g_au8TxData[g_u8DataLen++]);
            I2C_SET_CONTROL_REG(I2C0, I2C_CTL_SI);
        }
        else
        {
            I2C_SET_CONTROL_REG(I2C0, I2C_CTL_STO_SI);
            g_u8EndFlag = 1;
        }
    }
    else
    {
        /* TO DO */
        printf("Status 0x%x is NOT processed\n", u32Status);
    }
}

void I2C0_IRQHandler(void)
{
    uint32_t u32Status;

    u32Status = I2C_GET_STATUS(I2C0);

    if(I2C_GET_TIMEOUT_FLAG(I2C0))
    {
        /* Clear I2C0 Timeout Flag */
        I2C_ClearTimeoutFlag(I2C0);
    }
    else
    {
        if(s_I2CHandlerFn != NULL)
            s_I2CHandlerFn(u32Status);
    }
}

int main(){
	HalInit();
	Init_LCD();
	  clear_LCD();
	
    print_Line(0, "NuTiny - Nano102");
	  print_Line(1, "Cortex-M0 @32MHz");
	  print_Line(2, "Ultra Low Power ");
	  print_Line(3, "0.96 OLED 128x64");	

    print_C(0, 0, 'N');
    print_C(0, 1, 'C');
    print_C(0, 2, 'U');
    print_C(0, 3, ' ');
	while(1){
    print_C(0, 2, 'U');

	}
	return 0;
}